package com.qqhf.security;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.qqhf.common.utils.HttpKit;
import com.qqhf.hitfood.bean.entity.AcctInfo;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class JwtUtil {
    // 过期时间60分钟
    private static final long EXPIRE_TIME = 60 * 60 * 1000;

    /**
     * 校验token是否正确
     *
     * @param token  密钥
     * @param secret 用户的密码
     * @return 是否正确
     */
    public static boolean verify(String token, String username, String secret) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim("username", username)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 获取token中的账号信息
     *
     * @param token
     * @return
     */
    public static AcctInfo getAcctInfo(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Integer id = jwt.getClaim("id").asInt();
            String name = jwt.getClaim("name").asString();
            String password = jwt.getClaim("password").asString();
            Integer roleId = jwt.getClaim("roleId").asInt();
            Integer stId = jwt.getClaim("stId").asInt();
            Integer shopId = jwt.getClaim("shopId").asInt();
            return new AcctInfo(id, name, password, roleId, stId, shopId);
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static AcctInfo getAcctInfo() {
        return getAcctInfo(HttpKit.getToken());
    }

    public static Long getUserId() {
        return getUserId(HttpKit.getToken());
    }

    public static Long getUserId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userId").asLong();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 生成签名,5min后过期
     *
     * @param acctInfo 账号信息
     * @return 加密的token
     */
    public static String sign(AcctInfo acctInfo) {
        try {
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            Algorithm algorithm = Algorithm.HMAC256(acctInfo.getPassword());
            // 附带username信息
            return JWT.create()
                    .withClaim("id", acctInfo.getId())
                    .withClaim("name", acctInfo.getName())
                    .withClaim("password", acctInfo.getPassword())
                    .withClaim("roleId", acctInfo.getRoleId())
                    .withClaim("stId", acctInfo.getStId())
                    .withClaim("shopId", acctInfo.getShopId())
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}
